
<template>
  <div class="add">
    <el-form :model="addData" label-width="100px" :rules="rules" ref="ruleForm">
      <el-form-item label="预约方式:">
        <el-select v-model="addData.reserveType">
          <el-option
            v-for="item in typeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="适用车场:" prop="parkId">
        <el-select v-model="addData.parkId" remote filterable :loading="loading" :remote-method="remoteMethod" placeholder="请输入车场名称搜索">
          <el-option
            v-for="item in parkOptions"
            :key="item.id"
            :label="item.parkName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item v-if="addData.reserveType == 1" label="车位IP:" prop="ipLists">
        <template v-for="(item,index) in ipLists">
          <el-input v-model="item.ip" style="margin: 6px 0;"></el-input>
          <span class="add-ip" v-if="ipLists.length > 1" @click="reduceIp(index)">-</span>
          <span class="add-ip" v-if="ipLists.length == (index + 1)" @click="addIp">+</span>
        </template>
      </el-form-item>

      <el-form-item v-if="addData.reserveType == 2" label="预约车位数:" prop="reserveSpaceCount">
        <el-input v-model="addData.reserveSpaceCount"></el-input>
      </el-form-item>

      <el-form-item v-if="addData.reserveType == 2" label="小车场编号:" prop="parkCode">
        <el-input v-model="addData.parkCode"></el-input>
      </el-form-item>

      <el-form-item label="预约费用:">
        07:00 - 11:00 <el-input v-model="addData.timeSlotList[0].amount" size="mini" style="width: 100px;"></el-input>
        11:00 - 17:00 <el-input v-model="addData.timeSlotList[1].amount" size="mini" style="width: 100px;"></el-input>
      </el-form-item>

      <el-form-item label="车场分成:" prop="parkDivide">
        <el-input v-model.number="addData.parkDivide" size="mini" style="width: 55px;"></el-input> %
      </el-form-item>
    </el-form>

    <p style="text-align: center;">
      <el-button @click="submit('ruleForm')" type="primary">确认</el-button>
    </p>


  </div>
</template>

<script>
  export default {
    data() {
      return {
        typeOptions: [
          {
            value: 1,
            label: '开放式预约'
          },
          {
            value: 2,
            label: '闭环式预约'
          }
        ],
        addData: {
          ledUrlList:[],
          reserveType: 1,
          timeSlotList: [
            {
              reserveBeginTime: '07:00:00',
              reserveEndTime: '11:00:00'
            },
            {
              reserveBeginTime: '11:00:00',
              reserveEndTime: '17:00:00'
            },
          ]
        },
        loading: false,
        parkOptions: [],
        ipLists:[{ip:null}],
        rules: {
          parkId: [{ required: true, message: '请选择停车场', trigger: 'blur' }],
          reserveSpaceCount: [{ required: true, message: '请输入预约车位数', trigger: 'blur' }],
          parkCode: [{ required: true, message: '请输入车场编号', trigger: 'blur' }],
          parkDivide: [
            { required: true, message: '请输入分成比例', trigger: 'blur' },
            { type: 'number', message: '分成比例为数字值', trigger: 'blur'},
            { pattern: /^(0|[1-9]\d?|100)$/, message: '范围在0-100', trigger: 'blur'}
          ],
        }
      }
    },

    mounted() {

    },

    methods: {
      remoteMethod(query) {
        this.loading = true;
        this.parkOptions = [];
        if (query !== '') {
          this.$axios({
            method: 'get',
            url: '/park/park-info/get-no-reserve-park',
            params: {parkName: query}
          }).then(res => {
            if(res.code == 200) {
              res.data.forEach(item => {
                this.parkOptions.push({parkName: item.parkName,id:item.id})
              })
            }
            this.loading = false;
          })
        } else {
          this.parkOptions = [];
        }
      },

      addIp() {
        this.ipLists.push({ip:null})
      },

      reduceIp(index) {
        this.ipLists.splice(index,1)
      },

      submit(formName) {
        this.$refs[formName].validate((valid) => {
          if(valid) {
            this.addData.reserveSpaceCount = this.ipLists.length;
            this.ipLists.forEach(item => {
              this.addData.ledUrlList.push(item.ip)
            })
            this.$axios({
              method: 'post',
              url: '/park/park-info/add-reserve-space',
              data: this.addData
            }).then((res) => {
              if(res.code == 200) {
                this.$message.success('添加预约车场成功');
                this.$emit('closeDialog');
                this.$emit('refresh');
              } else {
                this.$message.warning(res.desc)
              }
            })
          } else {
            return false
          }
        })

      }



    }

  }
</script>

<style scoped>
  .el-input {
    width: 200px;
  }

  .add-ip {
    font-size: 30px;
    color: red;
    vertical-align: middle;
  }

  .add-ip:hover {
    cursor: pointer;
  }



</style>
